﻿using System;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint.Utilities;
using System.Web;
using System.Web.UI;

namespace ConsultPoint.SharePoint.GoogleAnalyticsConnector.Layouts
{
    public partial class GoogleAnalyticsSettings : LayoutsPageBase
    {
       
        SPWeb _web = null;

        #region Properties
        private String SelectedValue
        {
            get
            {
                if (rdoInherits.Checked || txtAccountID.Text == String.Empty)
                    return Helper.Constants.INHERITS;
                else
                    return txtAccountID.Text;
            }

        }
        
        private string RedirectUrl
        {
            get
            {
                if (Request.QueryString["Source"] != null)
                    return Request.QueryString["Source"];
                return String.Empty;
            }
        }
        private SPWeb CurrentSPWeb
        {
            get
            {
                return SPContext.Current.Web;
            }
        }
        private string GetAccountID()
        {
            if (_web.Properties.ContainsKey(Helper.Constants.KEY))
                return _web.Properties[Helper.Constants.KEY];
            else
                return String.Empty;
        }
        #endregion

        #region override
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                _web = CurrentSPWeb;
                if (!IsPostBack)
                {
                    ApplyInitialValue();
                }
            }
            catch (Exception ex)
            {
                this.Controls.Clear();
                this.Controls.Add(new LiteralControl("Error : " + ex.Message));
            }
        }
        protected override void OnInit(EventArgs e)
        {
            this.rdoOverwrite.CheckedChanged += new EventHandler(rdo_CheckedChanged);
            this.rdoInherits.CheckedChanged += new EventHandler(rdo_CheckedChanged);
            this.btnCancel.Click += new EventHandler(btnCancel_Click);
            this.btnSave.Click += new EventHandler(btnSave_Click);
        }
        public override void Dispose()
        {
            if (_web != null)
                _web.Dispose();
        }
        #endregion override

        #region Event Handlers
        void btnSave_Click(object sender, EventArgs e)
        {
            ApplyValue(_web, SelectedValue, chkApplyToChildren.Checked);
            DoRedirect();
        }
        void btnCancel_Click(object sender, EventArgs e)
        {
            DoRedirect();
        }
        void rdo_CheckedChanged(object sender, EventArgs e)
        {
            txtAccountID.Enabled = rdoOverwrite.Checked;
            chkApplyToChildren.Enabled = rdoOverwrite.Checked;
        }
        #endregion Event Handlers

        #region Private
        void ApplyInitialValue()
        {
            string accountId = GetAccountID();
            if (accountId == Helper.Constants.INHERITS)
            {
                rdoInherits.Checked = true;
                txtAccountID.Enabled = false;
                chkApplyToChildren.Enabled = false;
            }
            else if (accountId != String.Empty)
            {
                rdoOverwrite.Checked = true;
                txtAccountID.Text = accountId;
            }
            else
            {
                rdoInherits.Checked = true;
                txtAccountID.Enabled = false;
                chkApplyToChildren.Enabled = false;
            }

            if (_web.IsRootWeb)
            {
                rdoInherits.Checked = false;
                rdoInherits.Enabled = false;
                rdoOverwrite.Checked = true;
                txtAccountID.Enabled = true;
                chkApplyToChildren.Enabled = true;
            }
        }
        void DoRedirect()
        {
            if (RedirectUrl != String.Empty)
                SPUtility.Redirect(RedirectUrl, SPRedirectFlags.Default, HttpContext.Current);
            else
                SPUtility.Redirect(SPContext.Current.Web.Url + "/_layouts/settings.aspx" , SPRedirectFlags.Default, HttpContext.Current);
        }
        void ApplyValue(SPWeb web, string value, bool recursive)
        {
            bool allowunsafe = _web.AllowUnsafeUpdates;
            web.AllowUnsafeUpdates = true;
            try
            {
                string accountid = SelectedValue;
                if (web.Properties.ContainsKey(Helper.Constants.KEY))
                    web.Properties[Helper.Constants.KEY] = value;
                else
                    web.Properties.Add(Helper.Constants.KEY, value);

                web.Properties.Update();
            }
            finally
            {
                web.AllowUnsafeUpdates = allowunsafe;
            }

            if (recursive)
                foreach (SPWeb subweb in web.Webs)
                    ApplyValue(subweb, Helper.Constants.INHERITS, recursive);
        }
        #endregion

    }
}
